<template>
	<view class="container">
		<list class="question_list">
			<cell class="question_item" v-for="(item,index) in dataList" :key="item.id" v-if="item.show">
				<view>
					{{item.title}}
				</view>
				<list class="answer_list">
					<cell class="answer_item" v-for="(ite,ind) in item.optionsList" :key="ite.id"
						@tap="selOptions(index,ind)" :class="ite.selected?'active_item':''">
						{{ite.options}}、{{ite.content}}
					</cell>

				</list>
			</cell>
		</list>
		<view class="bottom_div">
			<image class="bottom_img"  @tap="frontQuestion()"  src="../../static/icon_before.png"></image>
			<view @click="answerCard" class="bottom_item">
				<image src="../../static/icon_sheet.png"></image>
				<view>答题卡</view>
			</view>
			<view class="bottom_item" @tap="toAnswer()">
				<image src="../../static/icon_answer.png"></image>
				<view>答案</view>
			</view>
			<image v-show="!submitShow" @click="nextQuestion" class="bottom_img" src="../../static/icon_after.png">
			</image>
			<view v-show="submitShow" class="bottom_item" @click="submitData">
				<image src="../../static/icon_answer.png"></image>
				<view>提 交</view>
			</view>
		</view>

		<view class="pop_div" v-show="cardShow">
			<view class="pop_top_div">
				<view>答题卡</view>
				<list class="index_list">
					<cell v-for="(item,index) in dataList" @tap="toIndex(index)" :class="item.answer?'active':''">
						{{index+1}}</cell>
				</list>
			</view>
			<image @click="closeCard" src="../../static/icon_order_close.png"></image>
		</view>

	</view>

</template>

<script>
	// 在test.vue页面接受参数
	export default {
		onLoad: function(option) { //option为object类型，会序列化上个页面传递的参数
			this.id = option.id;
			console.log(this.id)
		},
		data() {
			return {
				id: 0,
				cardShow: false, //是否显示答题卡
				submitShow: false, //是否显示提交按钮
				dataList: [
				],
				selIndex: 0, //默认显示的题目下下标
			}
		},activated() {
			this.initData()
		},
		onReady: function() {
				uni.request({
					url: this.baseUrl+'getTopicList',
					method: 'POST',
					data: {
						'cid': parseInt(this.id)
					},
					header: {
						'content-type': 'application/x-www-form-urlencoded' //自定义请求头信息
					},
					success: (res) => {
						console.log(res);
						this.dataList = res.data.topicEntityList;
						// this.text = 'request success';
						
						for (var i = 0; i < this.dataList.length; i++) {
							if (i == this.selIndex) {
								this.dataList[i].show = true;
							} else {
								this.dataList[i].show = false;
							}
						}
					}
				});
			
		},
		methods: {
			initData: function() {
				
			},
			toAnswer:function(){
				//答案
				uni.navigateTo({
					url:'../answeranalysis/answeranalysis?index='+this.selIndex+'&id='+this.id
					
				})
			},
			toIndex: function(e) {
				//点击题目的下标，跳转过去
				this.selIndex = e;
				for (var i = 0; i < this.dataList.length; i++) {
					if (i == this.selIndex) {
						this.dataList[i].show = true;
					} else {
						this.dataList[i].show = false;
					}
				}
				//关闭答题卡
				this.cardShow = false;

			},
			answerCard: function(e) {
				//点击打开答题卡
				this.cardShow = true;
			},
			closeCard: function() {
				//关闭答题卡
				this.cardShow = false;
			},
			frontQuestion:function(){
				//显示上一题
				this.selIndex--;
				//把下一题的设置成显示
				for (var i = 0; i < this.dataList.length; i++) {
					if (i == this.selIndex) {
						this.dataList[i].show = true;
					} else {
						this.dataList[i].show = false;
					}
				}
			},
			nextQuestion: function() {
				//下一题
				//如果所有选项的是否选中都是否
				var sel = 0;
				for (var x = 0; x < this.dataList[this.selIndex].optionsList.length; x++) {
					if (this.dataList[this.selIndex].optionsList[x].selected == true) {
						sel = 1;

					}
				}
				//如果sel是0说明一个都没选
				if (sel == 0) {
					uni.showToast({
						title: '请选择该题目答案',
						duration: 2000,
						icon: 'none'
					});
					return;
				}
				//设置本题已经解答
				this.dataList[this.selIndex].answer = true;
				//显示下一题
				this.selIndex++;
				//把下一题的设置成显示
				for (var i = 0; i < this.dataList.length; i++) {
					if (i == this.selIndex) {
						this.dataList[i].show = true;
					} else {
						this.dataList[i].show = false;
					}
				}
			},
			selOptions: function(ind1, ind2) {
				//点击选项选中
				this.dataList[ind1].optionsList[ind2].selected = !this.dataList[ind1].optionsList[ind2].selected;
				
			},
			submitData: function() {
				//点击提交
				//章节id paperId ，题目id topicId、答案id optionsId、学生id stuId type:2
				for(var i=0;i<this.dataList.length;i++){
					var topicId=this.dataList[i].id;//题目id
					var options=this.dataList[i].optionsList;
						var optionsId=[];//答案id、可能是多个答案
						for(var j=0;j<options.length;j++){
							if(options[j].selected){
								optionsId.push(options[j].id);
							}
						}
						uni.request({
							url: this.baseUrl+'save',
							method: 'POST',
							data: {
							'paperId':this.id,//章节id
							'stuId':uni.getStorageSync('uid'),//学生id
							'type':2,//章节练习
							'topicId':topicId,//题目id
							'optionsId':optionsId,//答案id,如果是多选题传一个集合
							},
							header: {
								'content-type': 'application/x-www-form-urlencoded'
							},
							success: (res) => {
								console.log(res); 
							}
						});
				}
				uni.navigateTo({
					// url: "'../examResult/examResult?targetId='+this.cid+'&type=2'"
						url: '../examResult/examResult?targetId=' + this.id+'&type=2'
				});
				
			
				
			}
		},
		watch: {
			selIndex: function() {
				console.log(this.selIndex);
				if (this.selIndex == this.dataList.length - 1) {
					this.submitShow = true;
				}
			}
		}
	}
</script>

<style>
	.container {
		width: 100%;
	}

	.question_list {

		width: 100%;
	}

	.question_list .question_item view:nth-child(1) {
		height: 96rpx;
		line-height: 96rpx;
		font-size: 28rpx;
		color: #333333;
		line-height: 96rpx;
		margin-left: 20rpx;
		margin-right: 20rpx;
		font-weight: bold;
	}

	.question_list .question_item .answer_list {
		display: flex;
		flex-direction: column;


	}

	.question_list .question_item .answer_list .answer_item {
		background: #FFFFFF;
		height: 86rpx;
		margin-left: 20rpx;
		margin-right: 20rpx;
		border-radius: 10rpx;
		padding-left: 20rpx;
		padding-right: 20rpx;
		line-height: 86rpx;
		margin-bottom: 20rpx;
		font-size: 22rpx;
		color: #666666;
	}

	.question_list .question_item .answer_list .active_item {
		background: #e13b29;
		color: #FFFFFF;
	}

	.bottom_div {
		position: fixed;
		bottom: 0;
		height: 96rpx;
		background: #FFFFFF;
		border-top: 1px solid #DADADA;
		width: 100%;
		display: flex;
		justify-content: space-between;
		align-items: center;


	}

	.bottom_div .bottom_img {
		height: 80rpx;
		width: 80rpx;
		text-align: center;
		
		padding-left: 10rpx;
		padding-right: 10rpx;
	}

	.bottom_div .bottom_item {
		text-align: center;
		flex: 1;
	}

	.bottom_div .bottom_item image {
		width: 34rpx;
		height: 36rpx;
	}

	.bottom_div .bottom_item view {
		font-size: 22rpx;
		color: #666666;
	}

	.pop_div {
		background: rgba(0, 0, 0, 0.5);
		position: absolute;
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
		text-align: center;
		padding-right: 20rpx;
	}

	.pop_div image {
		width: 60rpx;
		height: 60rpx;
		position: absolute;
		bottom: 20rpx;
		text-align: center;
		margin-top: 10rpx;
	}

	.pop_div .pop_top_div {
		position: absolute;
		bottom: 100rpx;
		left: 20rpx;
		right: 20rpx;
		margin-right: 20rpx;
		padding-bottom: 30rpx;
		background: #FFFFFF;
		border-radius: 10rpx;


	}

	.pop_div .pop_top_div list {
		display: block;
		width: 100%;
		text-align: left;
		padding-left: 10rpx;
		padding-right: 10rpx;
	}

	.pop_div .pop_top_div list cell {
		width: 62rpx;
		height: 62rpx;
		display: inline-block;
		background: #f3f3f3;
		font-size: 20rpx;
		color: #333333;
		text-align: center;
		line-height: 62rpx;
		margin: 10rpx;
		border-radius: 10rpx;
	}

	.pop_div .pop_top_div list .active {
		background: #e13b29;
		color: #FFFFFF;
	}

	.pop_div .pop_top_div view {
		margin-top: 20rpx;
		height: 62rpx;
		line-height: 62rpx;
		color: #333333;
		font-size: 28rpx;
		text-align: center;
	}
</style>
